package com.peanut.baby.mvp.mycontent;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.QA;
import com.peanut.baby.mvp.BaseMvpFragment;
import com.peanut.baby.mvp.bind.BindMobileActivity;
import com.peanut.baby.mvp.login.LoginActivity;
import com.peanut.baby.mvp.main.expert.qa.QARecyclerAdapter;
import com.peanut.baby.mvp.qadetail.QADetailActivity;
import com.peanut.devlibrary.util.StringUtil;
import com.peanut.devlibrary.widget.pullrecycle.PullRecyclerView;
import com.peanut.devlibrary.widget.pullrecycle.layoutmanager.XLinearLayoutManager;
import com.tencent.mars.xlog.Log;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by anonymouslystudio on 2017/7/12.
 */

public class QaFragment extends BaseMvpFragment implements PullRecyclerView.OnRecyclerRefreshListener,
    QARecyclerAdapter.OnItemViewClickListener,
        QaContract.View{

    private static final String TAG = QaFragment.class.getSimpleName();
    @BindView(R.id.pull_recycler)
    PullRecyclerView pullRecyclerView;

    Unbinder unbinder;

    private int pageNo  = 1;
    private int pageCount = 5;

    private QARecyclerAdapter adapter;
    private List<QA>  qas;
    private QaPresenter presenter;

    private String userId = "";
    public static QaFragment newInstance(String userId) {
        QaFragment fg = new QaFragment();
        Bundle bundle = new Bundle();
        bundle.putString("userId",userId);
        fg.setArguments(bundle);
        return fg;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_qa, null);
        unbinder = ButterKnife.bind(this, rootView);


        return rootView;
    }

    @Override
    public void initData() {
        userId = getArguments().getString("userId");
        presenter = new QaPresenter(this,getActivity());

        qas = new ArrayList<>();
        adapter = new QARecyclerAdapter(getActivity(),qas);

        pullRecyclerView.setLayoutManager(new XLinearLayoutManager(getActivity()));
        pullRecyclerView.setAdapter(adapter);

        pullRecyclerView.enablePullRefresh(true);
        pullRecyclerView.enableLoadMore(true);
        pullRecyclerView.setOnRecyclerRefreshListener(this);

        adapter.setOnClickListener(this);
        pullRecyclerView.postRefreshing();


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onPullRefresh() {
        pageNo = 1;
        presenter.getQAList(userId,pageNo,pageCount);
    }

    @Override
    public void onLoadMore() {
        pageNo += 1;
        presenter.getQAList(userId,pageNo,pageCount);
    }

    @Override
    public void onViewItemClicked(View v, QA item) {
        QADetailActivity.start(getActivity(),item.id);
    }

    @Override
    public void onViewPraiseClicked(QA qa) {
        if(InitManager.getInstance().getUser() == null){
            LoginActivity.startForResult(getActivity());
            return;
        }
        if(StringUtil.isNullOrEmpty(InitManager.getInstance().getUser().userToken)){
            BindMobileActivity.startForResult(getActivity());
            return;
        }
        presenter.praiseQA(qa);
    }

    @Override
    public void onGetQAListSuccess(List<QA> _qas) {
        Log.d(TAG,"onGetQAListSuccess: result "+(_qas == null? "null":""+_qas.size()));
        stopRefreshAndLoad();
        if(_qas != null && !_qas.isEmpty()){
            if(pageNo == 1){
                qas.clear();
            }
            qas.addAll(_qas);
            adapter.notifyDataSetChanged();
            pullRecyclerView.enableLoadMore(_qas.size() >= pageCount); // 当剩余还有大于0页的数据时，开启上拉加载更多
        }
    }

    @Override
    public void onRequestFailed(String reason) {
        stopRefreshAndLoad();
        showToast(reason);

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onPraiseSuccess(QA qa) {
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onPraiseFailed(String reason) {
        adapter.notifyDataSetChanged();
        showToast(reason);
    }

    private void stopRefreshAndLoad(){
        pullRecyclerView.stopRefresh();
        pullRecyclerView.stopLoadMore();
    }

}
